package com.hxgis.oc.util.idw;

import cn.hutool.json.JSONObject;
import com.hxgis.oc.config.OConfig;
import com.hxgis.oc.util.geotools.GeoConvertor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Slf4j
@Setter
@Getter
public class IDWConvertor {
    GeoConvertor geoConvertor;

    File inputJsonFile;

    File tmpShpFile;
    File tmpTiffFile;
    File tmpClipTiffFile;

    File contourGeojsonFile;
    File contourShpFile;
    File pngFile;
    File outputFile;

    private float increaseValue = 0.0f;
    private float multiply_value = 1.0f;
    private float nodataValue = 0.0f;

    private int width;
    private int height;
    private int contourDis;
    private String filedName;
    private int ffx = 1;

    public IDWConvertor(){

    }

    public IDWConvertor(File outfile){
        if(outfile != null){
            init(outfile);
        }
    }

    public void init(File outfile) {
        outputFile = outfile;
        String rawFileName = outputFile.getAbsolutePath();
        tmpShpFile = new File(rawFileName + ".shp");
        tmpTiffFile = new File(rawFileName + ".tiff");
        tmpClipTiffFile = new File(rawFileName + "_clip.tiff");
        contourShpFile = new File(rawFileName + "_contour.shp");
        contourGeojsonFile = new File(rawFileName + ".geojson");
        pngFile = new File(rawFileName + ".png");
    }

    public boolean create() throws IOException {
        boolean result;

        geoConvertor.save(tmpShpFile);

        if(!tmpShpFile.exists()){
            return false;
        }

        //idw插值生成tiff图像
        GdalTools.idwGrid(tmpShpFile,filedName,width * ffx,height * ffx,tmpTiffFile,increaseValue,multiply_value,nodataValue);
        if(!tmpTiffFile.exists()){
            return false;
        }

        //裁剪tiff
        String cutLineShp = OConfig.getConfig().getShpPath() + "省.shp";
        GdalTools.cutGrid(tmpTiffFile, cutLineShp, tmpClipTiffFile, nodataValue);
        if(!tmpClipTiffFile.exists()){
            return false;
        }

        //转png
        GdalTools.tiff2png(tmpClipTiffFile,pngFile);
        result = pngFile.exists();

        if(!result){
            return false;
        }

        //等值线追踪
        if(contourDis > 0){
            //GdalTools.contour(tmpClipTiffFile, contourShpFile, contourDis, field);
            //result = contourShpFile.exists();

            GdalTools.contour(tmpClipTiffFile, contourGeojsonFile, contourDis, filedName);
            result = contourGeojsonFile.exists();
        }

        if(!result){
            return false;
        }

        //Shp转geojson
        // GdalTools.shp2geojson(contourShpFile.getAbsolutePath(), contourGeojsonFile.getAbsolutePath());

        result = pngFile.exists() && contourGeojsonFile.exists();
        if(result){
            FileOutputStream fos = new FileOutputStream(outputFile);
            JSONObject jsonObject = new JSONObject();
            String pngFileName = pngFile.getName();
            String pngInfoFileName = pngFileName + ".aux.xml";
            String geojsonFileName = contourGeojsonFile.getName();
            jsonObject.set("product",new String[]{
                pngFileName,
                pngInfoFileName,
                geojsonFileName
            });
            fos.write(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
            fos.close();
        }

        return result;
    }

    private void delete(File file){
        try{
            file.delete();
        }catch(Exception ignored){}
    }

    private void deleteShp(File file){
        if(file == null){
            return;
        }
        try{
            String fileName = file.getAbsolutePath().replace(".shp","");
            file.delete();
            delete(new File(fileName + ".dbf"));
            delete(new File(fileName + ".shx"));
            delete(new File(fileName + ".prj"));
            delete(new File(fileName + ".fix"));
        }catch(Exception ignored){}
    }

    public void clearTmp(){
        deleteShp(tmpShpFile);
        delete(tmpTiffFile);
        delete(tmpClipTiffFile);
        deleteShp(contourShpFile);
    }

    public void clear(){
        delete(outputFile);
        delete(pngFile);
        delete(contourGeojsonFile);
    }
}
